package org.neuedu.servlet;

import org.neuedu.bean.User;
import org.neuedu.service.UserService;
import org.neuedu.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserUpdatePaawordServlet",urlPatterns = "/user/password")
public class UserUpdatePaawordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User fly_user = (User) request.getSession().getAttribute("fly_user");
        Integer id = fly_user.getId();
        String pass = request.getParameter("pass");
        String repass = request.getParameter("repass");

        if(pass.equals(repass)){
            User user= new User();
            user.setPassword(pass);
            user.setId(id);
            UserService service=new UserServiceImpl();
            int i = service.updatePasswordInfo(user);
            if(i !=0){
                fly_user.setPassword(user.getPassword());
                request.getRequestDispatcher("/forward/login").forward(request,response);
            }else{
                request.setAttribute("publishFailMsg","修改失败");
                request.getRequestDispatcher("/forward/set").forward(request,response);
            }

        }else{
            request.setAttribute("publishFailMsg","两次密码不正确");
            request.getRequestDispatcher("/forward/set").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
